package com.bp.hearthealth.pulse.lnapps.bp_ui.bp_fragment.bp_lifestyle.bp_adapter

import android.annotation.SuppressLint
import android.app.Activity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bp.hearthealth.pulse.lnapps.databinding.LifeStyleItemLayoutBgBinding
import com.bp.hearthealth.pulse.lnapps.bp_ui.bp_fragment.bp_lifestyle.bp_bean.BpLifeStyleBean
import com.bp.hearthealth.pulse.lnapps.bp_utils.DoubleClickListener

class BpLifeStyleArticelAdapter : RecyclerView.Adapter<BpLifeStyleArticelAdapter.ItemVH>() {
    private lateinit var onCliclLisener: OnItemClickListener
    private var dataList: List<BpLifeStyleBean> = arrayListOf()
    private lateinit var context: Activity

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemVH {
        return ItemVH(
            LifeStyleItemLayoutBgBinding.inflate(
                LayoutInflater.from(parent.context), parent, false
            )
        )
    }

    fun setListData(list: MutableList<BpLifeStyleBean>) {
        this.dataList = list
        notifyDataSetChanged()
    }

    inner class ItemVH(val binding: LifeStyleItemLayoutBgBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bindData(item: BpLifeStyleBean) {
            binding.ivArticleIcon.setImageResource(item.drawableId)
            binding.bean = item
        }
    }

    override fun onBindViewHolder(holder: ItemVH, @SuppressLint("RecyclerView") position: Int) {
        holder.bindData(dataList[position])
        holder.itemView.setOnClickListener(object : DoubleClickListener() {
            override fun onNoDoubleClick(v: View?) {
                onCliclLisener.onItemClick(dataList[position].tip, dataList[position].drawableId)
            }
        })
    }

    fun setOnItemClickLisener(clickListener: OnItemClickListener) {
        this.onCliclLisener = clickListener
    }

    interface OnItemClickListener {
        fun onItemClick(tip: String, drawableId: Int)
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    fun setActivity(activity: Activity) {
        this.context = activity
    }
}